package com.coldtech.circlecrash;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import com.coldtech.circlecrash.units.Unit;

public class Screen {
	private BufferedImage screen = null;
	private Graphics2D graphic = null;

	public Screen(CircleCrash game, int width, int height) {
		this.screen = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		this.graphic = screen.createGraphics();
		this.graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		this.graphic.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
	}

	public void drawSelection(Selection selection) {
		this.graphic.setColor(Color.LIGHT_GRAY);
		this.graphic.drawLine(selection.getMinX(), selection.getMinY(),
				selection.getMaxX(), selection.getMinY());
		this.graphic.drawLine(selection.getMaxX(), selection.getMinY(),
				selection.getMaxX(), selection.getMaxY());
		this.graphic.drawLine(selection.getMaxX(), selection.getMaxY(),
				selection.getMinX(), selection.getMaxY());
		this.graphic.drawLine(selection.getMinX(), selection.getMaxY(),
				selection.getMinX(), selection.getMinY());
	}

	public void drawUnit(Unit unit) {
		this.graphic.setColor(unit.getColor());
		this.graphic.drawOval(
				(int) (unit.getPosition().getX() - unit.getRadius()),
				(int) (unit.getPosition().getY() - unit.getRadius()),
				(int) unit.getSize(), (int) unit.getSize());
		if (unit.isSelected()) {
			this.graphic.drawOval(
					(int) (unit.getPosition().getX() - unit.getRadius()) + 1,
					(int) (unit.getPosition().getY() - unit.getRadius()) + 1,
					(int) unit.getSize() - 2, (int) unit.getSize() - 2);
		}
	}

	public void draw(Graphics2D g) {
		g.drawImage(screen, 0, 0, null);
		this.graphic.setColor(Color.BLACK);
		this.graphic.fillRect(0, 0, 500, 500);
	}
}
